package cn.yuhe.basic.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class EmailUtil {
    private static EmailUtil emailUtil;

    @Autowired
    private JavaMailSender javaMailSender;

    private JavaMailSender getJavaMailSender() {
        return javaMailSender;
    }

    private void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    @PostConstruct
    private void init() {
        emailUtil=new EmailUtil();
        emailUtil.setJavaMailSender(javaMailSender);
    }

    /**
    * @Description: 发送带附件邮件
    * @Title: SendEmail
    * @Author: BetterLWZ
    * @Version: 1.0
    * @Date:  2022/7/2 17:11
    * @Parameters: [to 收件人, subject 标题, text 内容支撑html, file 附件]
    * @Return void
    */
    public static void sendEmail(String to, String text,String fileName, File file) throws MessagingException {
        // 1.创建复杂邮件对象
        MimeMessage mimeMessage = emailUtil.getJavaMailSender().createMimeMessage();
        // 2.发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("betterlwz1998@163.com");
        helper.setSubject("宠物乐园");
        // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
        helper.setText(text,true);
        // 4.添加附件
        helper.addAttachment(fileName, file);
        // 5.设置收件人
        helper.setTo(to);
        // 6.发送邮件
        emailUtil.getJavaMailSender().send(mimeMessage);
    }

    /**
    * @Description: 发送纯文本邮件
    * @Title: SendTextEmail
    * @Author: BetterLWZ
    * @Version: 1.0
    * @Date:  2022/7/2 17:15
    * @Parameters: [to 收件人, subject 标题, text 内容]
    * @Return void
    */
    public static void sendTextEmail(String to, String text) throws MessagingException {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 1.1.设置发送人
        simpleMailMessage.setFrom("betterlwz1998@163.com");
        // 1.2.设置接收人
        simpleMailMessage.setTo(to);
        // 1.3.设置邮件主题
        simpleMailMessage.setSubject("宠物乐园");
        // 1.4.设置邮件文本内容
        simpleMailMessage.setText(text);
        // 使用邮件核心对象发送邮件
        emailUtil.getJavaMailSender().send(simpleMailMessage);
    }

    /**
    * @Description: 发送支撑html邮件
    * @Title: SendHtmlEmail
    * @Author: BetterLWZ
    * @Version: 1.0
    * @Date:  2022/7/2 17:17
    * @Parameters: [to, subject, text]
    * @Return void
    */
    public static void sendEmail(String to, String text) {
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = emailUtil.getJavaMailSender().createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("betterlwz1998@163.com");
            helper.setSubject("宠物乐园");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(text,true);
            // 5.设置收件人
            helper.setTo(to);
            // 6.发送邮件
            emailUtil.getJavaMailSender().send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    //public static void test(){
    //    System.err.println(emailUtil.getJavaMailSender());
    //}

}
